class CoolSound
{
	private m_soundHash:number;
	private m_Sound:egret.Sound;
	private m_channel:egret.SoundChannel;
	private m_callback:Function;
	private m_Obj:egret.DisplayObjectContainer;
	private m_isPlaying:boolean;
	public play(sound:egret.Sound,loop:number = 1)
	{
		if(sound != null)
		{
			this.m_isPlaying = true;
			this.m_Sound = sound;
			this.m_channel = this.m_Sound.play(0, loop);
			this.m_channel.addEventListener(egret.Event.SOUND_COMPLETE, this.onSoundComplete, this);
		}
	}
	public stop()
	{
		if( this.m_channel && this.m_isPlaying)
		{
			this.m_channel.stop();
		}
	}
	public setVolume(value:number)
	{
		if(this.m_channel != null)
		{
			this.m_channel.volume = value;
		}
	}
	public getIsPlaying():boolean
	{
		return this.m_isPlaying;
	}
	public addCompleteCallback(callback:Function, obj:egret.DisplayObjectContainer)
	{
		this.m_callback = callback;
		this.m_Obj = obj;
	}
	private onSoundComplete(event:egret.Event)
	{
		this.m_isPlaying = false;
        if(this.m_callback != null && this.m_Obj != null)
		{
			let callback = this.m_callback.bind(this.m_Obj);
			callback(event);
		}
    } 
}

class SoundEngine 
{
	private static s_SoundEngine:SoundEngine;
	public static getInstance():SoundEngine
	{
		if(this.s_SoundEngine == null)
		{
			this.s_SoundEngine = new SoundEngine();
		}
		return this.s_SoundEngine;
	}
	private m_canPlayMusic:boolean = true;
	private m_Music:CoolSound;
	private m_Effects:Array<CoolSound>;
	private m_EffectsKeys:Array<string>;
	public constructor() 
	{
		this.m_Effects = new Array();
		this.m_EffectsKeys = new Array();
	}
	public setVolume(tag:number, volume:number)
	{
		this.m_Effects[tag].setVolume(volume);
	}
	public setCanPlayMusic(value:boolean)
	{
		this.m_canPlayMusic = value;
	}
	public playMusic(str:string)
	{	
		if(!this.m_canPlayMusic)
		{
			return;
		}
		this.stopMusic();
		// let sound:egret.Sound = DataManager.getInstance().m_Sounds[str];
		// this.m_Music = new CoolSound();
		// this.m_Music.play(sound, -1);
		// this.m_Music.setVolume(0.3);
	}
	public stopMusic()
	{
		if(this.m_Music != null)
		{
			this.m_Music.stop();
		}
	}
	public getMusicIsPlaying()
	{
		let isPlaying = false;
		if(this.m_Music != null)
		{
			isPlaying = this.m_Music.getIsPlaying();
		}
		return isPlaying;
	}

	public playEffect(str:string,type:number = 0):number
	{
		let id:number;
		if(type == 0)
		{
			id = this.playHttpEffect(str);
		}
		else
		{
			id = this.playDiskEffect(str);
		}
		return id;
	}
	
	private playDiskEffect(str:string):number
	{
		let isHas:boolean = false;
		let effect:CoolSound;
		let id:number;
		let sound:egret.Sound = RES.getRes(str);
		for(let i = 0; i < this.m_EffectsKeys.length; i++)
		{
			if(this.m_EffectsKeys[i] == str)
			{
				isHas = true;
				id = i;
				break;
			}
		}
		if(isHas)
		{
			effect = this.m_Effects[id];
			effect.play(sound);
		}
		else
		{
			effect = new CoolSound();
			effect.play(sound);
			this.m_Effects.push(effect);
			this.m_EffectsKeys.push(str);
			id = this.m_Effects.length - 1;
		}
		console.log(this.m_Effects.length);
		return id;
	}
	
	private playHttpEffect(str:string):number
	{
		// let sound:egret.Sound = DataManager.getInstance().m_Sounds[str];
		// let effect = new CoolSound();
		// effect.play(sound);
		// this.m_Effects.push(effect);
		let id:number = this.m_Effects.length - 1;
		return id;
	}
	public stopEffect(id:number)
	{
		let effect = this.m_Effects[id];
		if(effect != null)
		{
			effect.stop();
		}
	}
	public stopAllEffects()
	{
		for(let i = 0; i < this.m_Effects.length; i++)
		{
			let effect = this.m_Effects[i];
			if(effect != null && effect.getIsPlaying())
			{
				effect.stop();
			}
		}
	}
}